﻿using Meirimoney.Exchange.Website.Utilities;
using SuperMoney.Datas;
using SuperMoney.Datas.DbModels;
using SuperMoney.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Meirimoney.Exchange.Website.Exchange
{
    public partial class Detail : LoginBase
    {
        /// <summary>
        /// 订单对象
        /// </summary>
        private ConsumeOrder order;

        public ConsumeOrder Order
        {
            get { return order; }
        }

        /// <summary>
        /// 兑换的商品对象
        /// </summary>
        private Commodity commodity;

        public Commodity Commodity
        {
            get { return commodity; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            long orderId = this.ParseOrderId("id");
            if (orderId > 0)
            {
                if (this.IsHttpGet)
                {
                    this.DataInitialize(orderId);
                }
                else if (IsHttpPost)
                {
                    ConsumeOrderService.Instance.Exchange(orderId);
                    this.Response.Redirect("List.aspx");
                }
            }
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        private void DataInitialize(long orderId)
        {
            this.order = ConsumeOrderService.Instance.GetSingleOrder(orderId);
            if (this.order != null)
            {
                //解码处理
                this.order.ShipAddress = this.Server.UrlDecode(this.order.ShipAddress);
                if (this.order.Status == (int)OrderStatus.Untreated)
                {
                    this.commodity = CommodityDbService.Instance.Get(this.order.CommodityId);
                }
                else
                {
                    this.ErrorHandle("当前订单已经处理完成！", true);
                }
            }
        }

        /// <summary>
        /// 解析订单编号
        /// </summary>
        /// <param name="key">订单的key</param>
        /// <returns></returns>
        private long ParseOrderId(string key)
        {
            long orderId = -1;
            if (!string.IsNullOrEmpty(this.QueryValue(key)))
            {
                long.TryParse(this.QueryValue(key), out orderId);
            }
            return orderId;
        }

        /// <summary>
        ///根据收货地址 返回支付宝账号信息
        /// </summary>
        /// <param name="shipaddress">收货地址</param>
        /// <returns>支付宝账号信息</returns>
        protected string[] AlipayAccount(string shipaddress)
        {
            string[] alipayAccount = shipaddress.Split(',');
            return alipayAccount;
        }
    }
}